﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestFlags.Question;

namespace LongestFlags.Answer
{
    class Sofia:ILongestFlag
    {
        public int GetLongestFlags(int[] iArray, out int startIndex, out int endIndex)
        {
            //throw new NotImplementedException();
            List<int> newlist = new List<int>();
            int count = 0;
            startIndex = 0;
            endIndex = 0;
            for (int i = 0; i < iArray.Length; i++)
            {
                if (iArray[i] > 0)
                {
                    if (newlist.Count > 0 && newlist[newlist.Count - 1] < 0)
                    {
                        newlist.Remove(-1);
                        count += 2;
                        endIndex = i;
                    }
                    else newlist.Add(1);
                }
                else if (iArray[i] < 0)
                {
                    if (newlist.Count > 0 && newlist[newlist.Count - 1] > 0)
                    {
                        newlist.Remove(1);
                        count += 2;
                        endIndex = i;
                    }
                    else newlist.Add(-1);
                }
                else { count++; endIndex += 1; }
            }
            if (newlist.Count > 0) startIndex = newlist.Count - 1;
            return count;
        }
    }
}
